const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('/')
    + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 获取今天的日期 YYYY-MM-DD
const getTodayDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

// 获取明天的日期 YYYY-MM-DD
const getTomorrowDate = (date, count) => {
  var tomDate = date;
  tomDate.setTime(tomDate.getTime() + count * 24 * 60 * 60 * 1000);
  const year = tomDate.getFullYear()
  const month = tomDate.getMonth() + 1
  const day = tomDate.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

/**
 * 获取两天相差的天数
 */
const getDiffDays = (startDate, endDate) => {
  var start = new Date(startDate);
  var end = new Date(endDate);
  var days = end.getTime() - start.getTime();
  if (days > 0) {
    return parseInt(days / (1000 * 60 * 60 * 24));
  } else {
    return -1;
  }
}

/**
 * 字符串转格式日期
 */
const getDateFormat = dateStr => {
  var date = new Date(dateStr);
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return month + "月" + day + "日";
}

function goPage(fn) {
  // if (gapTime == null || gapTime == undefined) {
  //   gapTime = 1500
  // }

  let _lastTime = null

  // 返回新的函数
  return function () {
    let _nowTime = + new Date()
    if (_nowTime - _lastTime > 1000 || !_lastTime) {
      fn.apply(this, arguments)   //将this和参数传给原函数
      _lastTime = _nowTime
    }
  }
}

module.exports = {
  formatTime: formatTime,
  getTodayDate: getTodayDate,
  getTomorrowDate: getTomorrowDate,
  getDiffDays: getDiffDays,
  getPhone: '18960066359',
  goPage: goPage,
  getDateFormat: getDateFormat,
}